import WMTS from 'ol/source/WMTS'
import TileLayer from "ol/layer/Tile"
import WMTSTileGrid from 'ol/tilegrid/WMTS'
import {get as getProjection } from "ol/proj"
import { getWidth, getTopLeft } from "ol/extent"

var projection = getProjection('EPSG:4326');
var projectionExtent = projection.getExtent();
var size = getWidth(projectionExtent) / 256;
var resolutions = new Array(18);
var matrixIds = new Array(18);
matrixIds[0] = 0;
for (var z = 1; z < 22; ++z) { // 计算比例尺
    resolutions[z] = size / Math.pow(2, z);
    matrixIds[z] = 'EPSG:4326:' + (z - 1);
}

// http://8.136.236.190:30000/geoserver/gwc/demo/zhejiang:tb_land_3301?gridSet=EPSG:4326&format=image/png
// http://8.136.236.190:30000/geoserver/gwc/service/wmts?layer=zhejiang%3Atb_land_3301&style=&tilematrixset=EPSG%3A4326&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fpng&TileMatrix=EPSG%3A4326%3A9&TileCol=850&TileRow=171
var tileLayer = new TileLayer({
    source: new WMTS({
        url: 'http://8.136.236.190:30000/geoserver/gwc/service/wmts?',
        layer: 'zhejiang:tb_land_3301',
        matrixSet: 'EPSG:4326',
        format: 'image/png',
        style: 'custome-polygon',
        projection: projection,
        tileGrid: new WMTSTileGrid({
            origin: getTopLeft(projectionExtent),
            resolutions: resolutions,
            matrixIds: matrixIds
        }),
        wrapX: true
    })
})

var loadWMTS = {
    tileLayer: tileLayer
}

export default loadWMTS